package com.webledger.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javassist.bytecode.stackmap.BasicBlock.Catch;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBClass {

	private Connection connection;
	private static String connectionString = "jdbc:mysql://192.168.2.100:3306/webleager",
			user = "root", password = "logicall";
	private static DBClass db = new DBClass();

	private DBClass() {
		
	}

	

	public static DBClass getDB() {
		return db;
	}

	public Connection getConnection() {
		Context initContext;
		try {
			if (connection == null || connection.isClosed()) {
				initContext = new InitialContext();
				Context envContext = (Context) initContext.lookup("java:/comp/env");
				DataSource ds = (DataSource) envContext.lookup("jdbc/WebLedger");
				connection = ds.getConnection();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

	public Connection closeConnection() throws SQLException {

		connection.close();
		return connection;

	}
}
